package com.hugo.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hugo.commonutiles.R;
import com.hugo.eduservice.entity.EduCourse;
import com.hugo.eduservice.entity.EduTeacher;
import com.hugo.eduservice.entity.vo.CourseInfoVo;
import com.hugo.eduservice.entity.vo.CoursePublishVo;
import com.hugo.eduservice.entity.vo.CourseQuery;
import com.hugo.eduservice.entity.vo.TeacherQuery;
import com.hugo.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hugo
 * @since 2020-09-08
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    // 查询所有课程列表
    // 课程列表 完善条件查询带分页功能
    @PostMapping("pageCourse/{current}/{limit}")
    public  R pageCourse(@PathVariable long current ,@PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pagecourse=new Page<>(current,limit);
        //构造条件
        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        eduCourseService.page(pagecourse,queryWrapper);
        List<EduCourse> list = pagecourse.getRecords();
        long total=pagecourse.getTotal();
        return R.ok().data("total",total).data("list",list);
    }
    //添加课程信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = eduCourseService.saveCourseInfoVo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }
    //查询课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public  R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.UpdateCourseInfo(courseInfoVo);
        return R.ok();
    }
    // 根据课程id查询课程 确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public  R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=eduCourseService.publishCourseInfo(id);
        return R.ok().data("coursePublish",coursePublishVo);
    }
    // 最终发布课程
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }
    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

}

